package com.yanqu.road.server.cmd.banquet;

import com.yanqu.road.entity.banquet.UserBanquetData;
import com.yanqu.road.entity.banquet.UserBanquetMember;
import com.yanqu.road.pb.banquet.BanquetProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.manger.banquet.BanquetMgr;
import com.yanqu.road.server.pb.BanquetPb;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;

import java.math.BigDecimal;
import java.util.Map;

//@Cmd(code = Protocol.S_BANQUET_MEMBER_LIST, desc = "请求宴会成员列表")
public class GetBanquetMemberCmd extends AbstractCommand {

	private String banquetId;

	private int groupNum = 0;
	private int groupId = 0;

	@Override
	public boolean parseData(YanQuMessage packet) throws Exception {
		BanquetProto.BanquetMemberInfoReq reqMsg = BanquetProto.BanquetMemberInfoReq.parseFrom(packet.getBytes());
		banquetId = reqMsg.getBanquetId();
		groupNum = reqMsg.getGroupNum();
		groupId = reqMsg.getGroupId();
		return true;
	}

	@Override
	public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
		if(BanquetMgr.isBanquetOver(banquetId)){
			ret = GameErrorCode.E_BANQUET_IS_OVER;
		}
	}

	@Override
	public void sendData(GamePlayer player) {
		BanquetProto.BanquetMemberInfoResp.Builder respMsg = BanquetProto.BanquetMemberInfoResp.newBuilder();
		if(ret != 0){
			respMsg.setRet(ret);
			player.sendPacket(Protocol.U_BANQUET_MEMBER_LIST, respMsg);
			return;
		}
		//进入宴会场景
		BanquetMgr.addBanquetScene(player.getUserId(),banquetId);
		//分批发送宴会成员，第一次通过协议返回，后面通过同步返回
		UserBanquetData banquet = BanquetMgr.getBanquet(banquetId);
		int memNum = 1000;//一次下发多少人
		int length = BanquetMgr.getBanquetMemberCount(banquetId);
		int sendTime = BigDecimal.valueOf(length).divide(BigDecimal.valueOf(memNum),0,BigDecimal.ROUND_UP).intValue();
		Map<Long, UserBanquetMember> userBanquetMemberMap = BanquetMgr.getBanquetMemberMap(banquetId,memNum,1);
		respMsg = BanquetPb.parseBanquetMemberListRespMsg(player,banquet,userBanquetMemberMap,length);
		respMsg.setRet(ret);
		respMsg.setCount(length);
		player.sendPacket(Protocol.U_BANQUET_MEMBER_LIST, respMsg);
		if(sendTime > 1){
			BanquetMgr.syncBanquetMemberTask(player.getUserId(),banquetId,sendTime,memNum,2);
		}
	}

	//直接下发客户端等待时间太久了
	public void sendDataImm(GamePlayer player) {
		BanquetProto.BanquetMemberInfoResp.Builder respMsg = BanquetProto.BanquetMemberInfoResp.newBuilder();
		if(ret != 0){
			respMsg.setRet(ret);
			player.sendPacket(Protocol.U_BANQUET_MEMBER_LIST, respMsg);
		}
		//进入宴会场景
		BanquetMgr.addBanquetScene(player.getUserId(),banquetId);
		//分批发送宴会成员，第一次通过协议返回，后面通过同步返回
		UserBanquetData banquet = BanquetMgr.getBanquet(banquetId);
		int memNum = 1000;//一次下发多少人
		int length = BanquetMgr.getBanquetMemberCount(banquetId);
		int sendTime = BigDecimal.valueOf(length).divide(BigDecimal.valueOf(memNum),0,BigDecimal.ROUND_UP).intValue();
		if(sendTime == 0)sendTime = 1;
		for(int i=1;i<=sendTime;i++){
			if(i == 1){
				Map<Long, UserBanquetMember> userBanquetMemberMap = BanquetMgr.getBanquetMemberMap(banquetId,memNum,i);
				respMsg = BanquetPb.parseBanquetMemberListRespMsg(player,banquet,userBanquetMemberMap,length);
				respMsg.setRet(ret);
				respMsg.setCount(length);
				player.sendPacket(Protocol.U_BANQUET_MEMBER_LIST, respMsg);
			}else{
				Map<Long, UserBanquetMember> userBanquetMemberMap = BanquetMgr.getBanquetMemberMap(banquetId,memNum,i);
				if(!BanquetMgr.syncBanquetMember(player,banquet,userBanquetMemberMap)){
					break;
				}
			}
		}
	}
}
